package cn.lds.healthj377.service.impl;

import cn.lds.healthj377.domain.HealthExamPackage;
import cn.lds.healthj377.domain.PackageItem;
import cn.lds.healthj377.dto.DsHealthConstant;
import cn.lds.healthj377.dto.ResponseDTO;
import cn.lds.healthj377.exception.SysException;
import cn.lds.healthj377.mapper.HealthExamPackageMapper;
import cn.lds.healthj377.mapper.PackageItemMapper;
import cn.lds.healthj377.service.HealthExamPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class HealthExamPackageServiceImpl implements HealthExamPackageService {

    @Autowired
    private HealthExamPackageMapper healthExamPackageMapper;

    @Autowired
    private PackageItemMapper packageItemMapper;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public ResponseDTO<List<HealthExamPackage>> selectPackage(HealthExamPackage healthExamPackage) throws SysException {

        List<HealthExamPackage> healthExamPackages = healthExamPackageMapper.selectPackage(healthExamPackage);

        if (CollectionUtils.isEmpty(healthExamPackages)){
//            return ResponseDTO.buildError(DsHealthConstant.HealthExamPackage.QUERY_ERROR);
            throw new SysException(ResponseDTO.buildError(DsHealthConstant.HealthExamPackage.QUERY_ERROR));
        }


        return ResponseDTO.buildSuccess(healthExamPackages);
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public ResponseDTO<HealthExamPackage> selectPackageById(Integer id) throws SysException {

        HealthExamPackage healthExamPackage = healthExamPackageMapper.selectPackageById(id);

        if (Objects.isNull(healthExamPackage)){
            throw new SysException(ResponseDTO.buildError(DsHealthConstant.HealthExamPackage.QUERY_ERROR));
//            return ResponseDTO.buildError(DsHealthConstant.HealthExamPackage.QUERY_ERROR);
        }

        return ResponseDTO.buildSuccess(healthExamPackage);
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResponseDTO addPackage(HealthExamPackage healthExamPackage,Integer... itemId) throws SysException {

//        healthExamPackage.setCreateTime(LocalDateTime.now());
        //添加套餐
        int i = healthExamPackageMapper.addPackage(healthExamPackage);
        int j = 0;
        //添加体检类目
        for (Integer im : itemId) {
            PackageItem packageItem = new PackageItem(healthExamPackage.getId(),im);
            j = packageItemMapper.addPackageItem(packageItem);
        }
        if (i * j == 0){
            throw new SysException(ResponseDTO.buildError(DsHealthConstant.SysMsg.ADD_ERR));
        }

        return ResponseDTO.buildSuccess();
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResponseDTO modifyPackage(HealthExamPackage healthExamPackage) {

//        healthExamPackage.setModifyTime(LocalDateTime.now());

        int i = healthExamPackageMapper.modifyPackage(healthExamPackage);

        if (i <= 0){

        }

        return ResponseDTO.buildSuccess();
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResponseDTO removePackageById(Integer id) {

        int i = healthExamPackageMapper.removePackageById(id);

        if (i<=0){

        }
        return ResponseDTO.buildSuccess();
    }
}
